package com.csii.adhoc.dataready.serviceImpl;

import com.csii.adhoc.dataready.TDataConstant;
import com.csii.adhoc.dataready.TDataFldPo;
import com.csii.adhoc.dataready.mapper.TDataFldMapper;
import com.csii.adhoc.dataready.service.TDataFldService;
import com.csii.adhoc.dataunits.addcolumn.DataNewFld;
import com.csii.adhoc.dataunits.mapper.TDataNewFldMapper;
import com.csii.adhoc.exception.ParamValidityException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @description: 数据集接口实现类
 * @author: zyliu
 * @create: 2021-01-13 09:32
 */
@Service("tDataFldService")
@Slf4j
public class TDataFldServiceImpl implements TDataFldService {

    @Autowired
    TDataFldMapper tDataFldMapper;
    @Autowired
    TDataNewFldMapper tDataNewFldMapper;


    @Override
    @Transactional
    public void tDataFldMaintain(TDataFldPo tDataFldPo, String optMode) {
        //检查上传的值

        switch (optMode) {
            case TDataConstant.OPT_MODE_ADD:
                insertOneTDataFld(tDataFldPo);
                break;
            case TDataConstant.OPT_MODE_UPDATE:
                updateTDataFldByPrimaryKey(tDataFldPo);
                break;
            case TDataConstant.OPT_MODE_DELETE:
                deleteByModelId(tDataFldPo.getModelId());
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("操作标志不合法:" + optMode);
        }
    }

    @Transactional
    @Override
    public void insertOneTDataFld(TDataFldPo tDataFldPo) {
        if (StringUtils.isEmpty(tDataFldPo.getModelId())) {
            tDataFldPo.setModelId(UUID.randomUUID().toString().replace("-", ""));
        }
        tDataFldMapper.insertOneTDataFld(tDataFldPo);
    }

    @Transactional
    @Override
    public void insertListTDataFld(List<TDataFldPo> pos) {
        tDataFldMapper.insertListTDataFld(pos);
    }

    @Transactional
    @Override
    public int updateTDataFldByPrimaryKey(TDataFldPo tDataFldPo) {
        return tDataFldMapper.updateByPrimaryKeySelective(tDataFldPo);
    }

    @Transactional
    @Override
    public int deleteByModelId(String modelId) {
        return tDataFldMapper.deleteByModelId(modelId);
    }

    @Transactional
    @Override
    public int deleteTDataFldByPrimaryKey(String modelId,String fldEnname) {
        return tDataFldMapper.deleteTDataFldByPrimaryKey(modelId,fldEnname);
    }

    @Override
    public List<TDataFldPo> selectListTDataFld(String modelId) {
        return tDataFldMapper.selectListTDataFld(modelId);
    }

    @Override
    public TDataFldPo selectOneTDataFld(String modelId,String fldEnname) {
        return tDataFldMapper.selectOneTDataFld(modelId,fldEnname);
    }
    @Override
    public Map selCustDataDbTable(String sql){
        return tDataFldMapper.selCustDataDbTable(sql);
    }

    @Override
    public List<DataNewFld> selectListTDataNewFld(String modelId) {
        return tDataNewFldMapper.selectListDataNewFld(modelId);
    }
}
